package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ChatGroup;
import com.lzx.iteam.bean.ScheduleManager;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.provider.MessageDB;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * Created by shijunting on 15-1-29.
 */
public class ScheduleManagerMsg extends CcMsgStructure {

    private static final String TAG = "ScheduleManagerMsg";

    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;
    private int mMethod; // 0:日程管理员   1:审批人  2：共享成员

    public ScheduleManagerMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    public ScheduleManagerMsg(Message callback, Context context,int method) {
        mmCallback = callback;
        mContext = context;
        mMethod = method;
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<ScheduleManager> managers = new ArrayList<ScheduleManager>();
        try {
            JSONObject jsonObject=data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray = null;
            if (mMethod == 0){
                jsonArray = jsonObject.getJSONArray("managers");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i);
                    ScheduleManager scheduleManager = new ScheduleManager();
                    scheduleManager.setUserName(item.getString("user_name"));
                    scheduleManager.setUserId(item.getString("user_id"));
                    scheduleManager.setAvatar(item.getString("avatar"));
                    scheduleManager.setJob(item.getString("job"));
                    managers.add(scheduleManager);
                }
            }else if (mMethod == 1){
                jsonArray = jsonObject.getJSONArray("approvals");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i);
                    ScheduleManager scheduleManager = new ScheduleManager();
                    scheduleManager.setContact_id(item.getString("contact_id"));
                    scheduleManager.setUserName(item.getString("contact_name"));
                    scheduleManager.setUserId(item.getString("user_id"));
                    scheduleManager.setAvatar(item.getString("contact_image"));
                    if (item.has("is_attendance")){
                        scheduleManager.setAttendance(item.getString("is_attendance"));
                    }
                    managers.add(scheduleManager);
                }
            }else if (mMethod == 2){
                jsonArray = jsonObject.getJSONArray("users");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i);
                    ScheduleManager scheduleManager = new ScheduleManager();
                    scheduleManager.setUserName(item.getString("user_name"));
                    scheduleManager.setUserId(item.getString("user_id"));
                    scheduleManager.setAvatar(item.getString("avatar"));
                    scheduleManager.setJob(item.getString("job"));
                    managers.add(scheduleManager);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = managers;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
